package com.lion.appletrip;

import java.util.Random;

import cn.bmob.v3.listener.SaveListener;

import com.activeandroid.util.Log;
import com.lion.appletrip.utils.BmobDataPrint;
import com.lion.appletrip.utils.PreferenceUtils;
import com.xinbo.utils.RegexValidateUtil;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

public class RegistActivity extends Activity
{
	private EditText edPhone;
	private EditText edMima;
	private int numAt;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_registsa);
		Intent intent = getIntent();
		numAt = intent.getIntExtra("Num", -1);
		edPhone = (EditText) findViewById(R.id.ed_phonenum_regist);
		edMima = (EditText) findViewById(R.id.ed_mima_regist);
		findViewById(R.id.btn_sendmsg).setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v)
			{
				if (!TextUtils.isEmpty(edPhone.getText().toString()))
				{
					Toast.makeText(RegistActivity.this, "发送成功",
					        Toast.LENGTH_SHORT).show();
				} else
				{
					Toast.makeText(RegistActivity.this, "电话不能为空",
					        Toast.LENGTH_SHORT).show();
				}

			}
		});
		findViewById(R.id.btn_regit_yes).setOnClickListener(
		        new OnClickListener()
		        {

			        @Override
			        public void onClick(View v)
			        {
				        String phone = edPhone.getText().toString().trim();
				        String psd = edMima.getText().toString().trim();
				        if (TextUtils.isEmpty(phone))
				        {
					        edPhone.setError("请输入手机号码");
					        return;
				        }
				        if (!RegexValidateUtil.checkMobileNumber(phone))
				        {
					        edPhone.setError("请输入正确的手机号码");
					        return;
				        }
				        if (TextUtils.isEmpty(psd))
				        {
					        Log.e("btn_regit", "psd=" + psd);
					        edMima.setError("请输入密码");
					        return;
				        }
				        if (!RegexValidateUtil.checkCharacter(psd))
				        {
					        Log.e("btn_regit", "psd2=" + psd);
					        edMima.setError("请输入4-16位密码");
					        return;
				        }
				        Random random = new Random();
				        int nextInt = random.nextInt(50);
				        String nichen = "面粉" + nextInt;
				        BmobDataPrint dataPrint = new BmobDataPrint(phone, psd,
				                nichen);
				        dataPrint.save(RegistActivity.this, new SaveListener()
				        {

					        @Override
					        public void onSuccess()
					        {

						        Toast.makeText(RegistActivity.this, "注册成功",
						                Toast.LENGTH_SHORT).show();
						        // 保存Bmob登入状态信息
						        PreferenceUtils.setLogin(RegistActivity.this,
						                true);
						        PreferenceUtils.setBomb(RegistActivity.this,
						        		true);
						        Intent intent = new Intent();
						        intent.putExtra("Login", numAt);
						        intent.setClass(RegistActivity.this,
						                MainActivity.class);
						        startActivity(intent);
						        finish();
					        }

					        @Override
					        public void onFailure(int arg0, String arg1)
					        {
						        Toast.makeText(RegistActivity.this,
						                "注册失败，请重新注册", Toast.LENGTH_SHORT)
						                .show();

					        }
				        });

			        }
		        });
		findViewById(R.id.btn_constont).setOnClickListener(new OnClickListener()
		{
			
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent();
                intent.setClass(
                        RegistActivity.this,
                        UserAgreelActivity.class);
                startActivity(intent);
				
			}
		});
		findViewById(R.id.btn_tologin_regist).setOnClickListener(new OnClickListener()
		{
			
			@Override
			public void onClick(View v)
			{
				finish();
				
			}
		});
		findViewById(R.id.btn_close_regist).setOnClickListener(new OnClickListener()
		{
			
			@Override
			public void onClick(View v)
			{
				finish();
				
			}
		});
	}

}
